#include "wap_menu.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_label.h"
#include "wa_anim.h"
#include "wa_anim_timeline.h"
#include "wa_input.h"
#include "wap.h"

static void wap_animation_create(void);
static void wap_animation_del(void);
static void wap_animation_callback(wa_sign* sign);

static void wap_animation_exec_cb(void* obj, int32_t value);
static void wap_animation_start_cb(wa_anim* anim);
static void wap_animation_finish_cb(wa_anim* anim);

wa_screen_obj wap_animation_screen = 
{
	.id = WAP_WIN_ANIMATION_ID,
	.create = wap_animation_create,
	.del = wap_animation_del,
};

wa_obj* wap_animation_win;
wa_obj* wap_animation_label;
wa_anim* wap_animation_anim[4];
wa_anim_timeline wap_animation_anim_timeline;
wa_anim_timeline_dsc wap_animation_anim_dsc[4];

extern wa_font font_song_16_ascii;

void wap_animation_init(void)
{
	
}

static void wap_animation_create(void)
{
	uint8_t i;
	wap_animation_win = wa_win_create(NULL);
	wa_win_set_pos(wap_animation_win, 0, 0);
	wa_win_set_size(wap_animation_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_animation_win, wa_nok);
	wa_win_set_callback(wap_animation_win, wap_animation_callback);
	wap_animation_screen.win = wap_animation_win;
	
	wap_animation_label = wa_label_create(NULL);
	wa_win_add_obj(wap_animation_win, wap_animation_label);
	wa_obj_set_pos(wap_animation_label, 96, 48);
	wa_obj_set_size(wap_animation_label, 32, 16);
	wa_label_set_font(wap_animation_label, &font_song_16_ascii);
	wa_label_set_text(wap_animation_label, "anim");
	wa_label_set_align_mode(wap_animation_label, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	
	for(i = 0; i < 4; i++)
	{
		wap_animation_anim[i] = wa_anim_create();
		wa_anim_set_var(wap_animation_anim[i], wap_animation_anim[i]);
		wa_anim_set_exec_cb(wap_animation_anim[i], wap_animation_exec_cb);
		wa_anim_set_start_cb(wap_animation_anim[i], NULL);
		wa_anim_set_finish_cb(wap_animation_anim[i], NULL);
		wa_anim_set_path_cb(wap_animation_anim[i], wa_anim_path_ease_in_out);
		
		wa_anim_set_time_long(wap_animation_anim[i], 500);
		wa_anim_set_early_apply(wap_animation_anim[i], wa_nok);
	}
	wa_anim_set_start_cb(wap_animation_anim[0], wap_animation_start_cb);
	wa_anim_set_finish_cb(wap_animation_anim[3], wap_animation_finish_cb);
	wa_anim_set_values(wap_animation_anim[0], 48, 0);
	wa_anim_set_values(wap_animation_anim[1], 0, 48);
	wa_anim_set_values(wap_animation_anim[2], 48, 0);
	wa_anim_set_values(wap_animation_anim[3], 0, 48);
	
	wa_anim_timeline_dsc_init(&wap_animation_anim_dsc[0], wap_animation_anim[0], 0);
	wa_anim_timeline_dsc_init(&wap_animation_anim_dsc[1], wap_animation_anim[1], 500);
	wa_anim_timeline_dsc_init(&wap_animation_anim_dsc[2], wap_animation_anim[2], 1000);
	wa_anim_timeline_dsc_init(&wap_animation_anim_dsc[3], wap_animation_anim[3], 1500);
	wa_anim_timeline_init(&wap_animation_anim_timeline, wap_animation_anim_dsc, 4);
}

static void wap_animation_del(void)
{
	uint8_t i;
	for(i = 0; i < 4; i++)
	{
		wa_anim_del(wap_animation_anim[i]);
	}
	wa_obj_del(wap_animation_win);
}

static void wap_animation_exec_cb(void* obj, int32_t value)
{
	uint8_t x, y;
	wa_anim* anim = (wa_anim*)obj;
	if(anim == wap_animation_anim[0])
	{
		y = value;
		x = value * 2;
		wa_obj_set_pos(wap_animation_label, x, y);
	}
	else if(anim == wap_animation_anim[1])
	{
		y = value;
		x = 0;
		wa_obj_set_pos(wap_animation_label, x, y);
	}
	else if(anim == wap_animation_anim[2])
	{
		y = value;
		x = 96 - value * 2;
		wa_obj_set_pos(wap_animation_label, x, y);
	}
	else if(anim == wap_animation_anim[3])
	{
		y = value;
		x = 96;
		wa_obj_set_pos(wap_animation_label, x, y);
	}
}

static void wap_animation_start_cb(wa_anim* anim)
{
	wa_input_enable(wa_nok);
}

static void wap_animation_finish_cb(wa_anim* anim)
{
	wa_input_enable(wa_ok);
}

static void wap_animation_start(void)
{
	wa_anim_timeline_start(&wap_animation_anim_timeline);
}

static void _wap_animation_paint(wa_obj* obj)
{
}

static void wap_animation_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_animation_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_ENTER:
		{
			wap_animation_start();
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

